import cv2
import numpy as np

if __name__ == '__main__':
    image = cv2.imread("C:\\Users\\20436\Desktop\yingpan.jpg")

    # 2. 图片镜像旋转
    flip_image = cv2.flip(
        image,  # 要反转的图像
        0,  # 翻转码
    )
    x_min,x_max=702,1148
    y_min,y_max=172,1155
    ROI_image = flip_image.copy()[y_min:y_max, x_min:x_max]
    points = np.float32([[111, 1], [444, 195], [1, 840], [393, 981]])
    copy_image = ROI_image.copy()
    img_shape = ROI_image.shape
    points1 = np.float32([[0, 0], [img_shape[1], 0], [0, img_shape[0]], [img_shape[1], img_shape[0]]])
    M = cv2.getPerspectiveTransform(
        points,
        points1
    )
    correct_image = cv2.warpPerspective(
        ROI_image,
        M,
        (img_shape[1], img_shape[0]),
        cv2.INTER_LINEAR,
        cv2.BORDER_WRAP
    )
    resize_image = cv2.resize(
        correct_image,  # 原图
        (400, 587),  # 目标分辨率(宽高)，如果设置这个参数，x轴和y轴的缩放比例失效
        fx=1,  # x轴倍率
        fy=1,  # y轴倍率
        interpolation=cv2.INTER_LINEAR  # 插值方法
    )

    cv2.imshow('open_image_np', resize_image)
    cv2.imwrite('open_image_np1.jpg', resize_image)
    cv2.waitKey(0)
